/*     */ package com.yizhilu.os.image.util;
/*     */ 
/*     */ import java.awt.Color;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Image;
/*     */ import java.awt.Rectangle;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import javax.imageio.ImageIO;
/*     */ 
/*     */ public class ImageHepler
/*     */ {
/*     */   public static void cut(String srcImageFile, String destImageFile, int width, int height, Rectangle rect)
/*     */     throws IOException
/*     */   {
/*  26 */     Image image = ImageIO.read(new File(srcImageFile));
/*  27 */     BufferedImage bImage = makeThumbnail(image, width, height);
/*     */ 
/*  32 */     saveSubImage(bImage, rect, new File(destImageFile));
/*     */   }
/*     */ 
/*     */   public static void cut(File srcImageFile, File destImageFile, int width, int height, Rectangle rect)
/*     */     throws IOException
/*     */   {
/*  47 */     Image image = ImageIO.read(srcImageFile);
/*  48 */     BufferedImage bImage = makeThumbnail(image, width, height);
/*     */ 
/*  51 */     saveSubImage(bImage, rect, destImageFile);
/*     */   }
/*     */ 
/*     */   private static BufferedImage makeThumbnail(Image img, int width, int height)
/*     */     throws IOException
/*     */   {
/*  64 */     BufferedImage tag = new BufferedImage(width, height, 1);
/*  65 */     Graphics g = tag.getGraphics();
/*  66 */     g.drawImage(img.getScaledInstance(width, height, 4), 0, 0, null);
/*     */ 
/*  68 */     g.dispose();
/*  69 */     return tag;
/*     */   }
/*     */ 
/*     */   private static void saveSubImage(BufferedImage image, Rectangle subImageBounds, File destImageFile)
/*     */     throws IOException
/*     */   {
/*  83 */     String fileName = destImageFile.getName();
/*  84 */     String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
/*  85 */     BufferedImage subImage = new BufferedImage(subImageBounds.width, subImageBounds.height, 1);
/*  86 */     Graphics g = subImage.getGraphics();
/*     */ 
/*  89 */     if ((subImageBounds.width > image.getWidth()) || (subImageBounds.height > image.getHeight()))
/*     */     {
/*  91 */       int left = subImageBounds.x;
/*  92 */       int top = subImageBounds.y;
/*  93 */       if (image.getWidth() < subImageBounds.width)
/*  94 */         left = (subImageBounds.width - image.getWidth()) / 2;
/*  95 */       if (image.getHeight() < subImageBounds.height)
/*  96 */         top = (subImageBounds.height - image.getHeight()) / 2;
/*  97 */       g.setColor(Color.white);
/*  98 */       g.fillRect(0, 0, subImageBounds.width, subImageBounds.height);
/*  99 */       g.drawImage(image, left, top, null);
/* 100 */       ImageIO.write(image, formatName, destImageFile);
/*     */     } else {
/* 102 */       g.drawImage(image.getSubimage(subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height), 0, 0, null);
/*     */     }
/*     */ 
/* 105 */     g.dispose();
/* 106 */     ImageIO.write(subImage, formatName, destImageFile);
/*     */   }
/*     */ }

/* Location:           F:\work\公司\质量学院\ducumentQIN\项目更新\web\20171122\
 * Qualified Name:     com.yizhilu.os.image.util.ImageHepler
 * JD-Core Version:    0.6.1
 */